package dietAndroidClient;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

import com.mnk.TCPsoketTest.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ProgActivity extends Activity implements OnClickListener {

	private ArrayList<TextView> sports;
	private ArrayList<String> goalAndDay;
	private ArrayList<String> datePeriod;
	private ArrayList<String> dateProgress;
	private ArrayList<String> accumNumber;
	private ArrayList<Double> progress;

	private final String GET_LIST = "SELECT * from goal_info";

	private Button calButton;
	private Button rankingButton;
	private Button settingButton;
	private Button addButton;

	Bundle bundle = new Bundle();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_1_1);

		calButton = (Button) findViewById(R.id.button2);
		rankingButton = (Button) findViewById(R.id.button3);
		settingButton = (Button) findViewById(R.id.button4);
		addButton = (Button) findViewById(R.id.button5);

		calButton.setOnClickListener(this);
		rankingButton.setOnClickListener(this);
		settingButton.setOnClickListener(this);
		addButton.setOnClickListener(this);

		Init();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.button2:
			// Change to Main Screen
			Intent cal = new Intent(this, CalendarView.class);
			startActivity(cal);
			finish();
			break;
		case R.id.button3:
			// Change to Main Screen
			Intent rank = new Intent(this, RankActivity.class);
			startActivity(rank);
			finish();
			break;
		case R.id.button4:
			// Change to Main Screen
			Intent set = new Intent(this, SettingActivity.class);
			startActivity(set);
			finish();
			break;
		case R.id.button5:
			// Change to Main Screen
			Intent add = new Intent(this, AddActivity.class);
			startActivity(add);
			finish();
			break;
		case R.id.textView1:
		case R.id.textView2:
			if (datePeriod.size() > 0) {
				bundle.putString("datePeriod", datePeriod.get(0));
				bundle.putString("goalAndDay", goalAndDay.get(0));
				bundle.putString("dateProgress", dateProgress.get(0));
				bundle.putString("accumNumber", accumNumber.get(0));
				bundle.putDouble("progress", progress.get(0));
			}

			Intent tv00 = new Intent(this, ItemPageActivity.class);
			tv00.putExtras(bundle);
			startActivityForResult(tv00, 0);

			break;
		case R.id.textView3:
		case R.id.textView4:
			if (datePeriod.size() > 1) {
				bundle.putString("datePeriod", datePeriod.get(1));
				bundle.putString("goalAndDay", goalAndDay.get(1));
				bundle.putString("dateProgress", dateProgress.get(1));
				bundle.putString("accumNumber", accumNumber.get(1));
				bundle.putDouble("progress", progress.get(1));
			}
			Intent tv01 = new Intent(this, ItemPageActivity.class);
			tv01.putExtras(bundle);
			startActivityForResult(tv01, 0);

			break;
		case R.id.textView5:
		case R.id.textView6:
			if (datePeriod.size() > 2) {
				bundle.putString("datePeriod", datePeriod.get(2));
				bundle.putString("goalAndDay", goalAndDay.get(2));
				bundle.putString("dateProgress", dateProgress.get(2));
				bundle.putString("accumNumber", accumNumber.get(2));
				bundle.putDouble("progress", progress.get(2));
			}
			Intent tv02 = new Intent(this, ItemPageActivity.class);
			tv02.putExtras(bundle);
			startActivityForResult(tv02, 0);

			break;
		case R.id.textView7:
		case R.id.TextView8:
			if (datePeriod.size() > 3) {
				bundle.putString("datePeriod", datePeriod.get(3));
				bundle.putString("goalAndDay", goalAndDay.get(3));
				bundle.putString("dateProgress", dateProgress.get(3));
				bundle.putString("accumNumber", accumNumber.get(3));
				bundle.putDouble("progress", progress.get(3));
			}
			Intent tv03 = new Intent(this, ItemPageActivity.class);
			tv03.putExtras(bundle);
			startActivityForResult(tv03, 0);

			break;

		}
	}

	public void Init() {

		sports = new ArrayList<TextView>();
		goalAndDay = new ArrayList<String>();
		datePeriod = new ArrayList<String>();
		dateProgress = new ArrayList<String>();
		accumNumber = new ArrayList<String>();
		progress = new ArrayList<Double>();

		sports.add((TextView) findViewById(R.id.textView1));
		sports.add((TextView) findViewById(R.id.textView2));
		sports.add((TextView) findViewById(R.id.textView3));
		sports.add((TextView) findViewById(R.id.textView4));
		sports.add((TextView) findViewById(R.id.textView5));
		sports.add((TextView) findViewById(R.id.textView6));
		sports.add((TextView) findViewById(R.id.textView7));
		sports.add((TextView) findViewById(R.id.TextView8));

		for (TextView text : sports) {
			text.setVisibility(View.INVISIBLE);
			text.setOnClickListener(this);
		}
		getDbData();
		sendDataToDB();

	}

	private void getDbData() {
		SQLiteDatabase db = null;
		Integer count = 0;
		if (db == null) {
			db = openOrCreateDatabase("goal_table.db",
					SQLiteDatabase.CREATE_IF_NECESSARY, null);
		}

		Cursor c = db.rawQuery(GET_LIST, null);
		startManagingCursor(c);
		String usage = "";
		
		while(c.moveToNext()) {

			if(c.getString(5).equals("YES") || 
					(c.getString(1).equals("") && c.getString(2).equals(""))) {
            	continue;
            }
			
			 // calculate Date
	         int duration = calculateTotalDate(c.getString(3), c.getString(2));
	         int current = calculateCurrentDate(c.getString(2)) + 1;
			
	         if((current - duration) > 0) {
	        	 continue;
	         }
			
			 if(c.getString(0).equals("턱걸이") || c.getString(0).equals("윗몸일으키기")) {
                 goalAndDay.add(c.getString(0) + " " + c.getString(1) + "개");
                 usage = "개";
	         } else {
	                 goalAndDay.add(c.getString(0) + " " + c.getString(1) + "Km");
	                 usage = "Km";
	         }
         
	         sports.get(count).setVisibility(View.VISIBLE);
	         sports.get(count).setText(c.getString(0) + "  " + c.getString(1) + usage + " ");
	         count++;

	         progress.add((double)current / (double)duration);
	         
	         sports.get(count).setVisibility(View.VISIBLE);
	         sports.get(count).setText("  " + current + "일차 / " + duration + "일");
	         
	         dateProgress.add("  " + current + "일차 / " + duration + "일");
	         datePeriod.add(c.getString(2) + " ~ " + c.getString(3));
	
	         int totalCount = Integer.parseInt(c.getString(1)) * current;
	         
	         if(c.getString(0).equals("턱걸이") || c.getString(0).equals("윗몸일으키기")) {
	                 accumNumber.add(totalCount + "개 / " + current + "일차");
	         } else {
	                 accumNumber.add(totalCount + "Km / " + current + "일차");
	         }
	         
	         count++; 
		} 


		Log.i("[DB Extract]", "count" + count);

		c.close();

		if (db != null) {
			db.close();
		}

	}

	private void sendDataToDB() {

		
		
	}
	
	public int calculateTotalDate(String start, String end) {

		Log.i("CalculateTotalDate", start + ", " + end);

		StringTokenizer st = new StringTokenizer(start, "-");

		Calendar startDate = Calendar.getInstance();
		startDate.set(Integer.parseInt(st.nextToken()),
				Integer.parseInt(st.nextToken()) - 1,
				Integer.parseInt(st.nextToken()));

		Log.i("DATE",
				startDate.get(Calendar.YEAR) + "-"
						+ (startDate.get(Calendar.MONTH) + 1) + "-"
						+ startDate.get(Calendar.DATE));

		st = new StringTokenizer(end, "-");

		Calendar endDate = Calendar.getInstance();
		endDate.set(Integer.parseInt(st.nextToken()),
				Integer.parseInt(st.nextToken()) - 1,
				Integer.parseInt(st.nextToken()));

		Log.i("DATE",
				endDate.get(Calendar.YEAR) + "-"
						+ (endDate.get(Calendar.MONTH) + 1) + "-"
						+ endDate.get(Calendar.DATE));

		int countDate = 0;

		if (!start.equals(end)) {
			while (!endDate.after(startDate)) {
				countDate++;
				endDate.add(Calendar.DATE, 1);
			}
		}

		Log.i("Date", "Date Difference is " + countDate);

		return countDate;
	}

	public int calculateCurrentDate(String start) {

		Date now = new Date();

		StringTokenizer st = new StringTokenizer(start, "-");

		Calendar startDate = Calendar.getInstance();
		startDate.set(Integer.parseInt(st.nextToken()),
				Integer.parseInt(st.nextToken()) - 1,
				Integer.parseInt(st.nextToken()));

		Log.i("DATE",
				startDate.get(Calendar.YEAR) + "-"
						+ (startDate.get(Calendar.MONTH) + 1) + "-"
						+ startDate.get(Calendar.DATE));

		Calendar today = Calendar.getInstance();
		today.setTime(now);

		Log.i("DATE",
				today.get(Calendar.YEAR) + "-"
						+ (today.get(Calendar.MONTH) + 1) + "-"
						+ today.get(Calendar.DATE));

		int countDate = 0;

		if (!startDate.equals(today)) {
			while (!startDate.after(today)) {
				countDate++;
				startDate.add(Calendar.DATE, 1);
			}
		}

		Log.i("Date", "Current Date Difference is " + countDate);

		return countDate;
	}

}
